package com.yunjing.mvparmsdemo.mvp.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.yunjing.mvparmsdemo.R;
import com.yunjing.mvparmsdemo.mvp.api.entity.NewListEntity;

import java.util.List;

public class NewListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final int ITEM_TYPE_PIC_NONE = 000; //第一种：条目没有图片
    private final int ITEM_TYPE_PIC01 = 666;    //第二种：条目中有一张图片
    private final int ITEM_TYPE_PIC02 = 888;    //第三种：条目中有两张图片
    private final int ITEM_TYPE_PIC03 = 999;    //第四种：条目中有三张图片

    private List<NewListEntity.ResultBean.DataBean> mData;
    private Context mContext;

    public void setData(List<NewListEntity.ResultBean.DataBean> data) {
        this.mData = data;
    }

    public NewListAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (TextUtils.isEmpty(mData.get(position).getThumbnail_pic_s()) &&
                TextUtils.isEmpty(mData.get(position).getThumbnail_pic_s02()) &&
                TextUtils.isEmpty(mData.get(position).getThumbnail_pic_s03())) {
            return ITEM_TYPE_PIC_NONE;
        } else if (!TextUtils.isEmpty(mData.get(position).getThumbnail_pic_s()) &&
                TextUtils.isEmpty(mData.get(position).getThumbnail_pic_s02()) &&
                TextUtils.isEmpty(mData.get(position).getThumbnail_pic_s03())) {
            return ITEM_TYPE_PIC01;
        } else if (!TextUtils.isEmpty(mData.get(position).getThumbnail_pic_s()) &&
                !TextUtils.isEmpty(mData.get(position).getThumbnail_pic_s02()) &&
                TextUtils.isEmpty(mData.get(position).getThumbnail_pic_s03())) {
            return ITEM_TYPE_PIC02;
        } else if (!TextUtils.isEmpty(mData.get(position).getThumbnail_pic_s()) &&
                !TextUtils.isEmpty(mData.get(position).getThumbnail_pic_s()) &&
                !TextUtils.isEmpty(mData.get(position).getThumbnail_pic_s03())) {
            return ITEM_TYPE_PIC03;
        } else {
            return super.getItemViewType(position);
        }

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        if (viewType == ITEM_TYPE_PIC_NONE) {
            return new ViewHolderPicNone(LayoutInflater.from(mContext).inflate(R.layout.item_new_list_pic_none, parent, false));
        } else if (viewType == ITEM_TYPE_PIC01) {
            return new ViewHolderPic01(LayoutInflater.from(mContext).inflate(R.layout.item_new_list_pic01, parent, false));
        } else if (viewType == ITEM_TYPE_PIC02) {
            return new ViewHolderPic02(LayoutInflater.from(mContext).inflate(R.layout.item_new_list_pic02, parent, false));
        } else if (viewType == ITEM_TYPE_PIC03) {
            return new ViewHolderPic03(LayoutInflater.from(mContext).inflate(R.layout.item_new_list_pic03, parent, false));
        } else {
            return new ViewHolderPicNone(LayoutInflater.from(mContext).inflate(R.layout.item_new_list_pic_none, parent, false));
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        NewListEntity.ResultBean.DataBean mDataBean = mData.get(position);
        String authorName = mDataBean.getAuthor_name();
        String date = mDataBean.getDate();
        String title = mDataBean.getTitle();
        String thumbnailPicS = mDataBean.getThumbnail_pic_s();
        String thumbnailPicS02 = mDataBean.getThumbnail_pic_s02();
        String thumbnailPicS03 = mDataBean.getThumbnail_pic_s03();

        if (holder instanceof ViewHolderPicNone) {
            ViewHolderPicNone holderPicNone = (ViewHolderPicNone) holder;
            holderPicNone.mTvAuthorName.setText(authorName);
            holderPicNone.mTvTitleName.setText(title);
            holderPicNone.mTvDate.setText(date);
        } else if (holder instanceof ViewHolderPic01) {
            ViewHolderPic01 holderPic01 = (ViewHolderPic01) holder;
            holderPic01.mTvAuthorName.setText(authorName);
            holderPic01.mTvTitleName.setText(title);
            holderPic01.mTvDate.setText(date);
            Glide.with(mContext).load(thumbnailPicS).placeholder(R.mipmap.default_img).error(R.mipmap.default_img).into(holderPic01.mIvPic01);
        } else if (holder instanceof ViewHolderPic02) {
            ViewHolderPic02 holderPic02 = (ViewHolderPic02) holder;
            holderPic02.mTvAuthorName.setText(authorName);
            holderPic02.mTvTitleName.setText(title);
            holderPic02.mTvDate.setText(date);
            Glide.with(mContext).load(thumbnailPicS).placeholder(R.mipmap.default_img).error(R.mipmap.default_img).into(holderPic02.mIvPic01);
            Glide.with(mContext).load(thumbnailPicS02).placeholder(R.mipmap.default_img).error(R.mipmap.default_img).into(holderPic02.mIvPic02);
        } else if (holder instanceof ViewHolderPic03) {
            ViewHolderPic03 holderPic03 = (ViewHolderPic03) holder;
            holderPic03.mTvAuthorName.setText(authorName);
            holderPic03.mTvTitleName.setText(title);
            holderPic03.mTvDate.setText(date);
            Glide.with(mContext).load(thumbnailPicS).placeholder(R.mipmap.default_img).error(R.mipmap.default_img).into(holderPic03.mIvPic01);
            Glide.with(mContext).load(thumbnailPicS02).placeholder(R.mipmap.default_img).error(R.mipmap.default_img).into(holderPic03.mIvPic02);
            Glide.with(mContext).load(thumbnailPicS03).placeholder(R.mipmap.default_img).error(R.mipmap.default_img).into(holderPic03.mIvPic03);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(position, mDataBean);
                }
            }
        });

    }


    @Override
    public int getItemCount() {
        return mData != null ? mData.size() : 0;
    }

    class ViewHolderPicNone extends RecyclerView.ViewHolder {
        TextView mTvTitleName;
        TextView mTvAuthorName;
        TextView mTvDate;

        public ViewHolderPicNone(@NonNull View itemView) {
            super(itemView);
            mTvTitleName = itemView.findViewById(R.id.tv_title_name);
            mTvAuthorName = itemView.findViewById(R.id.tv_author_name);
            mTvDate = itemView.findViewById(R.id.tv_date);
        }
    }

    class ViewHolderPic01 extends RecyclerView.ViewHolder {
        TextView mTvTitleName;
        TextView mTvAuthorName;
        TextView mTvDate;
        ImageView mIvPic01;

        public ViewHolderPic01(@NonNull View itemView) {
            super(itemView);
            mTvTitleName = itemView.findViewById(R.id.tv_title_name);
            mTvAuthorName = itemView.findViewById(R.id.tv_author_name);
            mTvDate = itemView.findViewById(R.id.tv_date);
            mIvPic01 = itemView.findViewById(R.id.iv_pic01);
        }
    }

    class ViewHolderPic02 extends RecyclerView.ViewHolder {
        TextView mTvTitleName;
        TextView mTvAuthorName;
        TextView mTvDate;
        ImageView mIvPic01;
        ImageView mIvPic02;

        public ViewHolderPic02(@NonNull View itemView) {
            super(itemView);
            mTvTitleName = itemView.findViewById(R.id.tv_title_name);
            mTvAuthorName = itemView.findViewById(R.id.tv_author_name);
            mTvDate = itemView.findViewById(R.id.tv_date);
            mIvPic01 = itemView.findViewById(R.id.iv_pic01);
            mIvPic02 = itemView.findViewById(R.id.iv_pic02);

        }
    }

    class ViewHolderPic03 extends RecyclerView.ViewHolder {
        TextView mTvTitleName;
        TextView mTvAuthorName;
        TextView mTvDate;
        ImageView mIvPic01;
        ImageView mIvPic02;
        ImageView mIvPic03;

        public ViewHolderPic03(@NonNull View itemView) {
            super(itemView);
            mTvTitleName = itemView.findViewById(R.id.tv_title_name);
            mTvAuthorName = itemView.findViewById(R.id.tv_author_name);
            mTvDate = itemView.findViewById(R.id.tv_date);
            mIvPic01 = itemView.findViewById(R.id.iv_pic01);
            mIvPic02 = itemView.findViewById(R.id.iv_pic02);
            mIvPic03 = itemView.findViewById(R.id.iv_pic03);
        }
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, NewListEntity.ResultBean.DataBean dataBean);
    }

}
